package com.vietnam.power.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ExceptionController {
	private static Logger logger = Logger.getLogger(ExceptionController.class);
	@Autowired
	private MessageSource messageSource;

	@ExceptionHandler(Exception.class)
	public ModelAndView handleAllException(HttpServletRequest request, Exception ex) {
		ModelAndView mav = new ModelAndView("errorPage/errorPage");
		logger.error(ex);
		Locale currentLocale = LocaleContextHolder.getLocale();
		mav.addObject("message", messageSource.getMessage("page.exception.message", null, currentLocale));

		return mav;
	}

}
